package com.caiyouxi.msg.service.impl;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.msg.mapper.MessageSourcesMapper;
import com.caiyouxi.msg.model.MessageSources;
import com.caiyouxi.msg.service.MessageSourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class MessageSourcesServiceImpl extends BaseService implements MessageSourcesService {
    @Autowired
    private MessageSourcesMapper mapper;
    @Autowired
    private RedisService redisService;

    @Override
    public ResData delete_message(MessageSources bean) {
        return success(mapper.delete_message(bean.getId()));
    }

    @Override
    public ResData insert_message(MessageSources bean) {
        return success(mapper.insert_message(bean));
    }

    @Override
    public ResData select_single_message(MessageSources bean) {
        return success(mapper.select_single_message(bean.getId()));
    }

    @Override
    public ResData update_message(MessageSources bean) {
        return success(mapper.update_message(bean));
    }

    @Override
    public ResData select_all(MessageSources bean) {
        Integer pageSize = Integer.parseInt(redisService.get("sys.pagesize").toString());//后台获取默认每页多少调数据
        QueryParams pageList = new QueryParams();

        Integer p = bean.getPage_size();                            //查看前台是否给了每页几条数据
        if(p == 0 || p == null){
            p = pageSize;                                           //如果没给数据，则取默认数据
        }
        bean.setPage_size(p);
        pageList.setPage(bean.getPage());
        pageList.setTotal(mapper.get_count_message(bean));
        pageList.setList(mapper.select_all(bean));
        pageList.setPage(bean.getPage());
        return success(pageList);
    }
}
